package com.think.service;

import com.think.dao.mapper.UserDaoMapper;
import com.think.model.JwtUserFactory;
import com.think.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2018/5/25 0025.
 */
@Service
public class CustomUserService implements UserDetailsService {

    private static final Logger logger = LoggerFactory.getLogger(CustomUserService.class);

    @Autowired
    private UserDaoMapper userDaoMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDaoMapper.findByUserName(username);
        if(user == null){
            throw new UsernameNotFoundException("用户名："+ username + "不存在！");
        }
        User result = JwtUserFactory.create(user);
        logger.info( "Authorities are " + result.getAuthorities());

        return result;
    }
}
